/* ==================================
	Main
 ================================== */
package it.unibo.iss.domotica.system;
import java.util.ArrayList;

import it.unibo.platform.expert.SubjectNameService;
import it.unibo.is.common.IS09Util;
import it.unibo.iss.domotica.EDDomain.Sensor;
import it.unibo.iss.domotica.EDDomain.Transmitter;
import it.unibo.platform.lindaLike.LindaLike;

public class SysDomotichSensors{

	private ArrayList<Transmitter> transmitters;
	private Transmitter tx;

	public void doJob(){
		init();
		configureTheSystem();
		configure();
		start();
	}

	private void configureTheSystem() {
		transmitters = new ArrayList<Transmitter>();
				
//		String edName = "edIB0";
//		Sensor sensor = new Sensor(1, 500);
//		tx = new Transmitter(edName, sensor);
		
		// Elettrodomestici Indispensabili Consumo Basso
		for(int i = 0 ; i < 3 ; i++) {
			String edName = "edIB" + i;
			Sensor sensor = new Sensor(1, 500);
			Transmitter transmitter = new Transmitter(edName, sensor);
			transmitter.setName("transmitter" + edName);
			transmitters.add(transmitter);
		}

		// Elettrodomestici Superflui Consumo Basso
		for(int i = 0 ; i < 3 ; i++) {
			String edName = "edSB" + i;
			Sensor sensor = new Sensor(1, 500);
			Transmitter transmitter = new Transmitter(edName, sensor);
			transmitter.setName("transmitter" + edName);
			transmitters.add(transmitter);
		}

		// Elettrodomestici Indispensabili Consumo Medio
		for(int i = 0 ; i < 3 ; i++) {
			String edName = "edIM" + i;
			Sensor sensor = new Sensor(501, 1000);
			Transmitter transmitter = new Transmitter(edName, sensor);
			transmitter.setName("transmitter" + edName);
			transmitters.add(transmitter);
		}

		// Elettrodomestici Superflui Consumo Medio
		for(int i = 0 ; i < 3 ; i++) {
			String edName = "edSM" + i;
			Sensor sensor = new Sensor(501, 1000);
			Transmitter transmitter = new Transmitter(edName, sensor);
			transmitter.setName("transmitter" + edName);
			transmitters.add(transmitter);
		}

		// Elettrodomestici Indispensabili Consumo Alto
		for(int i = 0 ; i < 3 ; i++) {
			String edName = "edIA" + i;
			Sensor sensor = new Sensor(1001, 2000);
			Transmitter transmitter = new Transmitter(edName, sensor);
			transmitter.setName("transmitter" + edName);
			transmitters.add(transmitter);
		}

		// Elettrodomestici Superflui Consumo Alto
		for(int i = 0 ; i < 3 ; i++) {
			String edName = "edSA" + i;
			Sensor sensor = new Sensor(1001, 2000);
			Transmitter transmitter = new Transmitter(edName, sensor);
			transmitter.setName("transmitter" + edName);
			transmitters.add(transmitter);
		}		
	}

	protected void init(){
		//Properties used by the system  (TODO)
		//System.setProperty("observeSpace", "unset");	//automatically set by selectInput

		//Properties to show the internal behavior 
		System.setProperty("coreTrace", "unset");
		System.setProperty("medclTrace", "unset");
		System.setProperty("obsTrace", "unset");
		System.setProperty("udpTrace", "unset");
		System.setProperty("tcpTrace", "unset");
		System.setProperty("httpTrace", "unset");
		initSupport();
	}

	protected void initSupport(){
		IS09Util.init();
		((LindaLike)LindaLike.getSpace()).clean();
		SubjectNameService.init();
	}
	
	protected void configure(){
		/* TODO
		 * Define an application observer 
		 * that auto-register itself in the shared space
		 */
		//	 	new ApplicationInputSpaceObserver( );	//once created, it observes the shared space

		SubjectNameService.addSubject( "TCP", "receiver", "consumptionData", "127.0.0.1", 9070 );  
	}

	protected void start(){
		for(Transmitter tx : transmitters) {
			tx.start();
		}
//		tx.start();
	}

	public static void main(String args[]) throws Exception {
		SysDomotichSensors system = new SysDomotichSensors( );
		system.doJob();
	}

}//domoticaContact
